package com.zryc.shop.file;


import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

// 用于实现文件的上传和下载
@Controller
@RequestMapping("/minio")
public class FileUploadController {

    public static final String IMAGE_PATH = "/Users/zhaozhao/mall_shop/image/";//存放图片的磁盘位置
    public static final String VIDEO_PATH = "/Users/zhaozhao/mall_shop/video/";//存放视频的磁盘位置

    public static final String IMAGE_HTTP = "http://localhost:8000/minio/image/";
    /**
     * 封装了上传图片的功能
     * // @RequestPart 用于在文件上传的请求中指定http请求参数名
     *
     * @param file
     * @return
     */
    @PostMapping({"/upload"})
    @ResponseBody
    public ResponseResult uploadImage(@RequestPart("file") MultipartFile file, HttpServletRequest request) throws IOException {
        String PATH = IMAGE_PATH;
        if (!file.isEmpty()) { //判断文件不为空时才保存文件
            if (!new File(PATH).exists()) {
                new File(PATH).mkdirs();//不存在就创建这个路径
            }
            // 先获取上传的图片文件后缀；file.getOriginalFilename()获取原始文件名
            String extension = StringUtils.getFilenameExtension(file.getOriginalFilename());
            // 拼接一个不会重复的新文件名 2d4835513a81f6121189e1c3800eb1a3.jpg
            String filename = UUID.randomUUID().toString().replaceAll("-", "") + "." + extension;
            // 保存这个图片
            file.transferTo(new File(PATH + filename));
            Map<String, String> data = new HashMap<>();
            data.put("url", IMAGE_HTTP + filename);
            return new ResponseResult(200, "上传成功", data);
        } else {
            return new ResponseResult(400, "文件不能为空");
        }
    }

    // /file/image/8c1d263e9aa54b38a134ca7e9ef14524.jpg
    @GetMapping("/image/{filename}")
    @ResponseBody
    public void image(@PathVariable String filename, OutputStream outputStream) throws IOException {
        // IOUtils.copy 将一个文件流拷贝到另外一个流当中
        if (!new File(IMAGE_PATH + filename).exists())
            filename = "image_lose.jpg";//如果无法找到该图片，则显示无法加载该图片
        IOUtils.copy(new FileInputStream(IMAGE_PATH + filename), outputStream);
    }

    @GetMapping("/video/{filename}")
    @ResponseBody
    public void downloadVideo(@PathVariable String filename, OutputStream outputStream) throws IOException {
        // IOUtils.copy 将一个文件流拷贝到另外一个流当中
        if (!new File(VIDEO_PATH + filename).exists())
            filename = "image_lose.jpg";//如果无法找到该图片，则显示无法加载该图片
        IOUtils.copy(new FileInputStream(VIDEO_PATH + filename), outputStream);
    }
/*
    @PostMapping("/upload/video")
    public Map<String, Object> uploadVideo(@RequestPart MultipartFile file) {
        return null;
    }*/
}
